﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RapidWebDev.UI.DynamicPages;
using RapidWebDev.UI.Controls;
using RapidWebDev.Platform;
using RapidWebDev.Common;
using TestCenter.Platform.Apis;
using System.Web.UI.WebControls;

namespace TestCenter.Platform.Web.DynamicPages
{
    public class BulkAssignTestCaseToTestPlan : AggregatePanelPage
    {
        protected static readonly IAuthenticationContext authenticationContext = SpringContext.Current.GetObject<IAuthenticationContext>();
        protected static readonly ITestCenterApi testCenterApi = SpringContext.Current.GetObject<ITestCenterApi>();
        protected static readonly IHierarchyApi hierarchyApi = SpringContext.Current.GetObject<IHierarchyApi>();
        #region web control bind
        [Binding]
        protected ComboBox TestPlanSelector;
        #endregion

        public override void OnLoad(IRequestHandler sender, AggregatePanelPageEventArgs e)
        {
            TestPlanSelector.Items.Clear();
            if (authenticationContext.Session["CurrentProject"] != null)
            {
                string testProjectId = authenticationContext.Session["CurrentProject"].ToString();
                var q = hierarchyApi.GetAllChildren("3", new Guid(testProjectId));
                foreach (var tmp in q)
                {
                    TestPlanSelector.Items.Add(new ListItem(tmp.Name,tmp.Code));
                }
            }
        }

        public override void Save(string commandArgument, IEnumerable<string> entityIdEnumerable)
        {
            string testplanid = this.TestPlanSelector.SelectedValue;
            testCenterApi.AssignTestCaseToTestPlan(entityIdEnumerable,authenticationContext.TempVariables["suite"].ToString(), testplanid);
        }
        public override void SetupContextTempVariables(IRequestHandler sender, SetupApplicationContextVariablesEventArgs e)
        {
            if (sender.Parameters["suite"] != null)
                authenticationContext.TempVariables["suite"] = new Guid(sender.Parameters["suite"]);
        }
    }
}
